package com.op.studyingliteapp.fragment.notes;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.op.studyingliteapp.R;
import com.op.studyingliteapp.bean.NoteBean;

public class NotesAdapter extends BaseAdapter {
	private Context context;
	private List<NoteBean> list;

	public NotesAdapter(Context context, List<NoteBean> notesContentList) {
		this.context = context;
		this.list = notesContentList;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public String getNoteId(int position) {
		return list.get(position).getId();
	}
	public String getNoteContent(int position) {
		return list.get(position).getContent();
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		String item = list.get(position).getContent();
		String time = list.get(position).getCreateTime();
		ViewHolder viewHolder;
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_notes, null);
			viewHolder = new ViewHolder();
			viewHolder.notesIcon = (ImageView) convertView.findViewById(R.id.notes_icon);
			viewHolder.notesContent = (TextView) convertView.findViewById(R.id.notes_content);
			viewHolder.notesTime = (TextView) convertView.findViewById(R.id.note_time);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// 将笔记信息添加进去
		if (item.length()>10) {
			item=item.substring(0, 10);
		}
		viewHolder.notesContent.setText(item.replace(" ", "").replaceAll("\n", ""));
		convertView.setTag(viewHolder);
		viewHolder.notesTime.setText(time);

		return convertView;
	}

	// 每个item的控件定义
	private static class ViewHolder {
		private ImageView notesIcon;
		private TextView notesContent,notesTime;
	}
}
